package com.besall.allbase.view.activity.chipstoollevel4.protocol.fragments;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import  bb.lanxing.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class System extends Fragment implements View.OnClickListener {

    public String TAG = getClass().getSimpleName();
    private LinearLayout infoView1;
    private LinearLayout infoView2;
    public String[] cmds_1 = {};
    public String[] cmds_2 = {};
    private Context mContext;

    SystemCallBack mCallBack;
    public System() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = inflater.getContext();

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_system, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        infoView1 = getActivity().findViewById(R.id.system_view1);
        infoView2 = getActivity().findViewById(R.id.system_view2);
        DisplayMetrics screen = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(screen);
        int width = screen.widthPixels;

        for (int i = 0; i < cmds_1.length; i++) {
            View view = new View(mContext);
            view.setId(i);
            view.setLayoutParams(new ViewGroup.LayoutParams((int) (width * 0.45), 20));

            Button button = new Button(mContext);
            button.setText("    " + cmds_1[i]);
            button.setId(i);
            button.setOnClickListener(this);
            button.setTextSize(12);
            button.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            button.setGravity(Gravity.CENTER_VERTICAL);
            button.setBackgroundResource(R.drawable.rectangle_cardbtn);


            infoView1.addView(button);
            infoView1.addView(view);
        }

        for (int i = 0; i < cmds_2.length; i++) {
            View view = new View(mContext);
            view.setId(i);
            view.setLayoutParams(new ViewGroup.LayoutParams((int) (width * 0.45), 20));

            Button button = new Button(mContext);
            button.setText("    " + cmds_2[i]);
            button.setId(i + 100);
            button.setOnClickListener(this);
            button.setTextSize(12);
            button.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            button.setGravity(Gravity.CENTER_VERTICAL);
            button.setBackgroundResource(R.drawable.rectangle_cardbtn);

            infoView2.addView(button);
            infoView2.addView(view);
        }
    }

    Toast toast;
    @Override
    public void onClick(View v) {
        if (mCallBack != null){
            mCallBack.onSystemClick(v.getId());
            Log.i(TAG, "onClick: " + v.getId());
            int id = v.getId();
            if (toast != null)
                toast.cancel();
            if (id < 100){
                toast = Toast.makeText(mContext, cmds_1[id],Toast.LENGTH_SHORT);
                toast.show();
            } else if (id > 100) {
                toast = Toast.makeText(mContext, cmds_2[id - 100],Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }

    public void setSystemCallBack(SystemCallBack systemCallBack){
        mCallBack = systemCallBack;
    }

    public interface SystemCallBack{

        void onSystemClick(int id);
    }
}
